﻿using Api.Core.Auth;
using Api.Core.Help;
using Api.Core.IServices.Module;
using Api.Core.IServices.Role;
using Api.Core.Model;
using Api.Core.Model.Module;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Api.Core.Controllers
{

    public class ModuleController : BaseController
    {
        private readonly IAuthServer _authServer;
        private readonly IRoleModuleInfoService _roleModuleInfoService;
        private readonly IModuleInfoService _moduleInfoService;

        public ModuleController(IModuleInfoService moduleInfoService
            , IRoleModuleInfoService roleModuleInfoService
            , IAuthServer authServer
           )
        {
            _authServer = authServer;
            _roleModuleInfoService = roleModuleInfoService;
            _moduleInfoService = moduleInfoService;
        }

        [HttpGet("allMenu")]
        public async Task<IActionResult> GetAllMenu([FromQuery]BasePage basePage)
        {
            var module = await _moduleInfoService.Query();
            var data = MenuEntityToDto.GetTreeApi(module);
            var Children = data.Take(basePage.pageSize * basePage.pageIndex).Skip(basePage.pageSize * (basePage.pageIndex - 1)).ToList();
            var totalCount = data.Count();
            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / basePage.pageSize.ObjToDecimal())).ObjToInt();
            var page = new PageModel<ModuleDto>()
            {
                page = basePage.pageIndex,
                pageCount = pageCount,
                dataCount = totalCount,
                PageSize = basePage.pageSize,
                Children = Children
            };
            return Ok(page);
        }


        [HttpGet("apis")]
        public async Task<IActionResult> GetApi([FromQuery]string oldToken)
        {
            var userModel = _authServer.SerializeJwt(oldToken);
            if (userModel == null)
            {
                return BadRequest("用户认证失败，请重新登陆");
            }
            var list = await _moduleInfoService.GetMenus(userModel.UserId);
            var MenuList = MenuEntityToDto.GetTreeApi(list);
            return Ok(MenuList);
        }


        [HttpGet("apiAll")]
        public async Task<IActionResult> GetAllApi()
        {
            var list = await _moduleInfoService.Query();
            var MenuList = MenuEntityToDto.GetTreeApi(list);
            return Ok(MenuList);
        }

        [HttpGet("apiById")]
        public async Task<ModuleEntity> GetApiById(int id)
        {
            var list = await _moduleInfoService.Query(x => x.Id == id);
            return list.FirstOrDefault();
        }

        [HttpPost("addModule")]
        public async Task<IActionResult> AddModule(ModuleEntity moduleEntity)
        {
            moduleEntity.Enabled = true;
            var isTrue = await _moduleInfoService.Add(moduleEntity);
            return new JsonResult(new
            {
                sussess = isTrue
            });
        }

        [HttpPost("delById")]
        public async Task<IActionResult> DelModuleById([FromQuery]int id)
        {
            var isTrue = await _moduleInfoService.DeleteById(id);
            return new JsonResult(new
            {
                sussess = isTrue
            });
        }


        [HttpPost("addRoleModule")]
        public async Task<IActionResult> AddRoleModule([FromQuery]int id, string ids)
        {
            var isTrue = await _moduleInfoService.AddRoleModule(id, ids);
            return new JsonResult(new
            {
                sussess = isTrue
            });
        }


        [HttpPost("delModuleById")]
        public async Task<IActionResult> DelRoleModuleById([FromQuery]int RoleId, int ModuleId)
        {
            var roleModule = await _roleModuleInfoService.Query(x => x.RoleId == RoleId && x.ModuleId == ModuleId);
            var isTrue = await _roleModuleInfoService.DeleteById(roleModule.FirstOrDefault().Id);
            return new JsonResult(new
            {
                sussess = isTrue
            });
        }
    }
}